/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.interpreter.backends.rete;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Laurent WOUTERS
 */
public class BetaNCCEntryNode implements TokenHolder, TokenActivable {
    private TokenHolder betaMem;
    private TokenActivable child;
    private BetaNCCExitNode exit;
    
    public BetaNCCExitNode getExitNode() { return exit; }
    
    public BetaNCCEntryNode(TokenHolder beta) {
        this.betaMem = beta;
        this.exit = new BetaNCCExitNode(beta);
        this.betaMem.addChild(this);
    }
    
    @Override public void activateToken(Token token) {
        exit.preActivation(token);
        child.activateToken(token);
        exit.postActivation(token);
    }
    @Override public void deactivateToken(Token token) {
        exit.preDeactivation(token);
        child.deactivateToken(token);
        exit.postDeactivation(token);
    }
    @Override public void activateTokens(Collection<Token> tokens) {
        exit.preActivation(tokens);
        child.activateTokens(new FastBuffer<Token>(tokens));
        exit.postActivation(tokens);
    }
    @Override public void deactivateTokens(Collection<Token> tokens) {
        exit.preDeactivation(null);
        child.deactivateTokens(new FastBuffer<Token>(tokens));
        exit.postDeactivation(tokens);
    }

    @Override public Collection<Token> getTokens() { return this.betaMem.getTokens(); }
    @Override public Collection<TokenActivable> getChildren() {
        Collection<TokenActivable> list = new ArrayList<TokenActivable>();
        if (child != null)
            list.add(child);
        return list;
    }
    @Override public void addChild(TokenActivable activable) { this.child = activable; }
    @Override public void removeChild(TokenActivable activable) { this.child = null; }
    @Override public void removeAllChildren() { this.child = null; }
}
